<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\User as UserModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(['education', 'workExperience']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('mobile');
            $grid->column('avatar_url')->display(function () {
              return $this->defaultAvatar();
            })->image('',60);
            $grid->column('name');
            $grid->column('gender')->using(UserModel::GENDER_LIST)->dot(
                [
                    1 => 'primary',
                    2 => 'pink',
                    3 => 'success',
                ],
            );
            $grid->column('birthday');
            $grid->column('identity')->using([0=>'未知',...UserModel::IDENTITY_LIST])->dot(
                [
                    1 => 'primary',
                    2 => 'danger',
                    3 => 'success',
                    4 => 'orange2',
                    5 => 'red',
                ],
            );
            $grid->column('self_description')->limit(5, '...');
            $grid->column('education', '教育经历')
                ->display('查看') // 设置按钮名称
                ->expand(function () {
                    if ($this->education) {
                        $card =
                            "学校：{$this->education->school_name}学历：{$this->education->degree}毕业时间：{$this->education->graduation_date}专业：{$this->education->major}";
                    } else {
                        $card = '暂无';
                    }
                    return "<div style='padding:10px'>$card</div>";
                });
            $grid->column('workExperience', '工作经历')
                ->display('查看') // 设置按钮名称
                ->expand(function () {
                    $card = '';
                    foreach ($this->workExperience as $item){
                        $card .= "<p>公司名称：{$item->company_name}职位名称：{$item->job_title}时间：{$item->start_date}-{$item->end_date}职位描述：{$item->job_description}</p>";
                    }
                    return "<div style='padding:10px'>$card</div>";
                });
            $grid->column('is_lock')->using([0 => '正常', 1 => '锁定'])->label([
                0 => 'primary',
                1 => 'danger'
            ])->editable()->select([0 => '正常', 1 => '锁定']);
            $grid->column('last_ip');
            $grid->column('last_at');
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('mobile')->width(2);
                $filter->like('name')->width(2);
            });
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            $grid->disableRowSelector();
            //禁用新增按钮
            $grid->disableCreateButton();
//            // 禁用过滤器按钮
//            $grid->disableFilterButton();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            $grid->disableEditButton();
            $grid->disableActions();
            // 禁用显示按钮
            $grid->disableViewButton();
            // 去掉删除
            $grid->disableDeleteButton();
            //显示横向滚动条
            $grid->scrollbarX();


        });

    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('mobile')->required()->rules("unique:user,mobile,{$form->model()->id},id");
            $form->image('avatar_url')->uniqueName()->autoUpload()->required()->removable(false)->saveFullUrl();
            $form->text('name');
            $form->radio('gender')->options(UserModel::GENDER_LIST)->default(0);
            $form->date('birthday');
            $form->radio('identity')->options(UserModel::IDENTITY_LIST)->default(0);
            $form->textarea('self_description');
            $form->radio('is_lock', '是否锁定')->options([0 => '正常', 1 => '锁定'])->default(0);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
